package com.shanzmoo.job.task;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanzmoo.core.util.RedisCoreKeyUtils;
import com.shanzmoo.job.base.BaseTask;
import com.shanzmoo.db.picture.entity.MallBannerPictureEntity;
import com.shanzmoo.db.picture.service.IMallBannerPictureService;
import com.shanzmoo.db.product.entity.ProductCategoryEntity;
import com.shanzmoo.db.product.entity.dto.ProductCategoryDTO;
import com.shanzmoo.db.product.entity.vo.IndexCategoryVO;
import com.shanzmoo.db.product.service.IProductCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商城缓存刷新
 *
 * @author： Gzhao 2021/1/22
 */
@Slf4j
@Component(value = "mallOtherRedisRefreshTask")
public class MallOtherRedisRefreshTask implements BaseTask {

    /**
     * banner 图 redis key
     */
    private static final String BANNER_KEY = "szm:mp:banner";
    /**
     * 首页分类 redis key
     */
    private static final String INDEX_CATEGORY_KEY = "szm:mp:spu:category:index";
    /**
     * 所有分类 redis key
     */
    private static final String ALL_CATEGORY_KEY = "szm:mp:spu:category:all";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IMallBannerPictureService mallBannerPictureService;
    @Autowired
    private IProductCategoryService productCategoryService;

    @Override
    public Object doJob() {

        refreshBannerRedis();

        refreshProductCategoryRedis();

        return "success";
    }

    /**
     * 刷新商品分类缓存
     *
     * @author Gzhao 2021/1/22
     */
    private void refreshProductCategoryRedis() {
        log.debug("开始刷新商城分类缓存");

        List<ProductCategoryEntity> firstCategoryList = productCategoryService.list(
                new QueryWrapper<ProductCategoryEntity>()
                        .eq("deleted", false)
                        .eq("status", true)
                        .eq("shelves", true)
                        .eq("category_level", 1)
                        .orderByDesc("id")
        );
        List<String> reListStr = firstCategoryList.stream()
                .map(e -> {
                    return JSON.toJSONString(IndexCategoryVO.create(e));
                }).collect(Collectors.toList());

        stringRedisTemplate.delete(RedisCoreKeyUtils.installIndexCategoryKey());
        stringRedisTemplate.opsForList().leftPushAll(RedisCoreKeyUtils.installIndexCategoryKey(), reListStr);

        List<ProductCategoryDTO> allCategoryList = productCategoryService.getAllCategory();

        List<String> reListStr2 = allCategoryList.stream()
                .map(JSON::toJSONString)
                .collect(Collectors.toList());
        stringRedisTemplate.delete(RedisCoreKeyUtils.installAllCategoryKey());
        stringRedisTemplate.opsForList().leftPushAll(RedisCoreKeyUtils.installAllCategoryKey(), reListStr2);
    }

    /**
     * 刷新 首页banner 图缓存
     *
     * @author Gzhao 2021/1/22
     */
    private void refreshBannerRedis() {

        log.debug("开始刷新商城 banner 图缓存");

        List<MallBannerPictureEntity> list = mallBannerPictureService.list(
                new QueryWrapper<MallBannerPictureEntity>()
                        .eq("deleted", false)
        );

        List<String> result = list.stream().map(MallBannerPictureEntity::getPictureUrl).collect(Collectors.toList());

        stringRedisTemplate.delete(RedisCoreKeyUtils.installBannerKey());
        stringRedisTemplate.opsForList().leftPushAll(RedisCoreKeyUtils.installBannerKey(), result);
    }
}
